
package com.magicalcoder.youyamvc.app.logadminoperate.service.impl;

import com.magicalcoder.youyamvc.app.model.defined.IntegerDate;
import com.magicalcoder.youyamvc.app.model.defined.IntegerObject;
import com.magicalcoder.youyamvc.app.logadminoperate.dao.LogAdminOperateDao;
import com.magicalcoder.youyamvc.app.logadminoperate.service.LogAdminOperateService;
import com.magicalcoder.youyamvc.app.model.LogAdminOperate;
import com.magicalcoder.youyamvc.app.model.defined.LogAdminOperateWithAdminUser;
import com.magicalcoder.youyamvc.core.common.utils.ListUtils;
import com.magicalcoder.youyamvc.core.common.utils.ProjectUtil;
import com.magicalcoder.youyamvc.core.common.utils.StringUtils;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import com.magicalcoder.youyamvc.core.common.utils.copy.Copyer;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;

/**
* 代码为自动生成 Created by www.magicalcoder.com
* 如果你改变了此类 read 请将此行删除
* 欢迎加入官方QQ群:323237052
*/

@Component("logAdminOperateService")
public class LogAdminOperateServiceImpl implements LogAdminOperateService{
    @Resource(name="logAdminOperateDao")
    private LogAdminOperateDao logAdminOperateDao;


    @Override
    public LogAdminOperate getLogAdminOperate(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        return logAdminOperateDao.getLogAdminOperate(query);
    }

    @Override
    public LogAdminOperate selectOneLogAdminOperateWillThrowException(Map<String, Object> query){
        return logAdminOperateDao.getLogAdminOperate(query);
    }

    @Override
    public LogAdminOperate selectFirstLogAdminOperate(Map<String, Object> query){
        query.put("limit",1);
        List<LogAdminOperate> list = logAdminOperateDao.getLogAdminOperateList(query);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<LogAdminOperate> getLogAdminOperateList(Map<String, Object> query) {
        return logAdminOperateDao.getLogAdminOperateList(query);
    }

    @Override
    public Integer getLogAdminOperateListCount(Map<String, Object> query) {
        return logAdminOperateDao.getLogAdminOperateListCount(query);
    }

    @Override
    public     Long  insertLogAdminOperate(LogAdminOperate entity) {
        return logAdminOperateDao.insertLogAdminOperate(entity);
    }

    @Override
    public     Long  replaceLogAdminOperate(LogAdminOperate entity) {
        return logAdminOperateDao.replaceLogAdminOperate(entity);
    }

    @Override
    public void updateLogAdminOperate(LogAdminOperate entity) {
        logAdminOperateDao.updateLogAdminOperate(entity);
    }

    @Override
    public void updateLogAdminOperateWithoutNull(LogAdminOperate entity) {
        logAdminOperateDao.updateLogAdminOperateWithoutNull(entity);
    }

    @Override
    public void updateLogAdminOperateByWhereSql(Map<String,Object> entity,String whereSql) {
        if(entity==null ||entity.isEmpty()){
            throw new RuntimeException("entity不能为空");
        }
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        entity.put("whereSql",whereSql);
        logAdminOperateDao.updateLogAdminOperateByWhereSql(entity);
    }

    @Override
    public void deleteLogAdminOperate(Long id) {
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("id", id);
        logAdminOperateDao.deleteLogAdminOperate(query);
    }
    @Override
    public void deleteLogAdminOperateList(Map<String,Object> entity){
        logAdminOperateDao.deleteLogAdminOperateList(entity);
    }

    @Override
    public void deleteLogAdminOperateByWhereSql(String whereSql) {
        if(StringUtils.isBlank(whereSql)){
            throw new RuntimeException("whereSql不能为空");
        }
        Map<String,Object> query = new HashMap<String,Object>();
        query.put("whereSql", whereSql);
        logAdminOperateDao.deleteLogAdminOperateByWhereSql(query);
    }


    @Override
    public List<LogAdminOperateWithAdminUser> getLogAdminOperateWithAdminUserByAdminUserIdList(Map<String, Object> query){
        return logAdminOperateDao.getLogAdminOperateWithAdminUserByAdminUserIdList(query);
    }


    @Override
    public void truncateLogAdminOperate() {
        logAdminOperateDao.truncateLogAdminOperate();
    }

    @Override
    public void batchInsertLogAdminOperate(List<LogAdminOperate> list) {
        logAdminOperateDao.batchInsertLogAdminOperate(list);
    }

    @Override
    public void batchReplaceLogAdminOperate(List<LogAdminOperate> list) {
        logAdminOperateDao.batchReplaceLogAdminOperate(list);
    }

    @Override
    public void batchUpdateLogAdminOperate(List<LogAdminOperate> list) {
        logAdminOperateDao.batchUpdateLogAdminOperate(list);
    }
    @Override
    public void batchDeleteLogAdminOperate(List<Long> idList) {
        logAdminOperateDao.batchDeleteLogAdminOperate(idList);
    }

    @Override
    public void batchDeleteLogAdminOperateList(List<LogAdminOperate> entityList){
        logAdminOperateDao.batchDeleteLogAdminOperateList(entityList);
    }

    @Transactional
    @Override
    public void transactionImportJsonList(List<LogAdminOperate> list) {
        if(list!=null && list.size()>0){
            for(LogAdminOperate logAdminOperate : list){
                if (logAdminOperate.getId() == null) {
                    insertLogAdminOperate(logAdminOperate);
                } else {
                    LogAdminOperate entity = getLogAdminOperate(logAdminOperate.getId());
                    if(entity==null){
                        insertLogAdminOperate(logAdminOperate);
                    }else {
                        Copyer.copy(logAdminOperate, entity);
                        updateLogAdminOperate(entity);
                    }
                }
            }
        }
    }


    public List<LogAdminOperate> tryQueryList(String keyword,String selectValue, String foreignJavaField){
        List<LogAdminOperate> list;
        if(StringUtils.isBlank(keyword) || "全部".equals(keyword)|| "请选择".equals(keyword)){
            return getLogAdminOperateList(ProjectUtil.buildMap("start",0,"limit", 20));
        }else{
            if(ProjectUtil.isNum(keyword)){
            list = searchList("adminUserIdFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            }
            list = searchList("userNameFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            list = searchList("urlFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            list = searchList("formBodyFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
            list = searchList("createTimeFirst",keyword);
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return new ArrayList<LogAdminOperate>();
    }

    private List<LogAdminOperate> searchList(String field,String keyword){
        List<LogAdminOperate> list = getLogAdminOperateList(ProjectUtil.buildMap(field,keyword,"start",0,"limit", 20));
        if(ListUtils.isNotBlank(list)){
            return list;
        }
        String[] keys = keyword.split("-");
        for(String key:keys){
            list = getLogAdminOperateList(ProjectUtil.buildMap(field,key,"start",0,"limit", 20));
            if(ListUtils.isNotBlank(list)){
                return list;
            }
        }
        return null;
    }

    @Override
    public List<IntegerDate> statisticsDateCountList(Map<String, Object> query) {
    return logAdminOperateDao.statisticsDateCountList(query);
    }

    @Override
    public List<IntegerDate> statisticsDateIntervalList(Map<String, Object> query) {
        return logAdminOperateDao.statisticsDateIntervalList(query);
    }

    @Override
    public List<IntegerObject> statisticsGroupList(Map<String, Object> query) {
        return logAdminOperateDao.statisticsGroupList(query);
    }
}
